
#include "c_world.h"
#include "init.h"

#include "m_menu.h"


////////////////////////////////////////////////////////////////////////////////
opcd C_World::assemble_images()
{
    TexImage grass;

    opcd e;
    e = assemble_background(grass);
    if(e)  return e;

    //TexImage tmp;

    if( 0 != _spr_flame.load("data\\rflame.csa", TARGET_BPP) )
    {
        _spr_flame.compose2way_spec( TARGET_BPP, "_model\\mflamev", ".tga", 1, 1, 4, &grass, 8 );
        _spr_flame.save("data\\rflame.csa");
/*
        e = tmp.compose( "_model\\mflamev", ".tga", 1, 1, 4, 1 );
        if(e)  return e;

        _img_flame.create_from_template(tmp,TARGET_BPP,false);
        _img_flame.draw(tmp);
        _img_flame.writeRAW("data\\rflame.bwh");
*/
    }


    if( 0 != _spr_bomb.load("data\\rbomb.csa", TARGET_BPP) )
    {
        _spr_bomb.compose( TARGET_BPP, "_model\\mbomb", ".tga", 2, 1, 16, &grass );
        _spr_bomb.save("data\\rbomb.csa");

/*
        e = tmp.compose( "_model\\mbomb", ".tga", 2, 1, 4, 4 );
        if(e)  return e;

        _img_bomb.create_from_template(tmp,TARGET_BPP,false);
        _img_bomb.draw(tmp);
        _img_bomb.writeRAW("data\\rbomb.bwh");
*/
    }

    Menu::set_sel_image(&_spr_bomb);


    if( 0 != _spr_man.load("data\\rman.csa", TARGET_BPP) )
    {
        _spr_man.compose( TARGET_BPP, "_model\\man", ".tga", 2, 1, 4*NMANANIM, &grass );
        _spr_man.save("data\\rman.csa");
    }


    if( 0 != _spr_manq.load("data\\rmanq.csa", TARGET_BPP) )
    {
        _spr_manq.compose( TARGET_BPP, "_model\\manqa", ".tga", 2, 1, 12, &grass );
        _spr_manq.save("data\\rmanq.csa");
    }   


/*
    charstr mm = "data\\rman";
    uint lmm = mm.len();
    int n=1;

    for( int k=0; k<NMANANIM; ++k, n+=4 )
    {
        mm.trim_to_length(lmm);
        mm.append_num10( k, 1, charstr::ALIGN_NUM_FILL_WITH_ZEROS );
        mm << ".bwh";

        if( 0 == _img_man[k].readRAW(mm, TARGET_BPP) )  continue;

        e = tmp.compose( "_model\\man", ".tga", 2, n, 2, 2 );
        if(e)  return e;


        tmp.make_transparent(0xff000000);
        _img_man[k].create_from_template(tmp,TARGET_BPP,false);
        _img_man[k].draw(tmp);
        _img_man[k].writeRAW(mm);
    }
*/

    return 0;
}

////////////////////////////////////////////////////////////////////////////////
opcd C_World::assemble_background( TexImage& grass )
{
    opcd e = grass.readTGA("data\\mgrass.tga");
    if(e)  return e;

    _img_back.create_from_template(grass,TARGET_BPP,false);
    _img_back.draw(grass);

    _img_back.get( _imgsample_back, 0, 0, TWpx, TWpx );
    
    e = _img_wall.readRAW("data\\rwall.bwh", TARGET_BPP);
    if(!e)  return e;

    TexImage dst;

    dst.create( 4*TWpx, 4*TWpx, 32, true );
    dst.draw( grass, 0, 0 );
    dst.draw( grass, 2*TWpx, 0 );
    dst.draw( grass, 0, 2*TWpx );
    dst.draw( grass, 2*TWpx, 2*TWpx );

    TexImage tmp;

    e = tmp.readTGA("data\\mhard.tga");
    if(e)  return e;

    dst.draw( tmp, 0, 0, TexImage::DRAWMODE_ALPHA_BLIT );


    e = tmp.readTGA("data\\msoft.tga");
    if(e)  return e;

    dst.draw( tmp, 0, 2*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    
    //soft wall 
    TexImage si( dst, 0, 2*TWpx, TWpx, TWpx );

    TexImage msk;
    e = msk.readTGA("_model\\soft1.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, TWpx, 2*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft2.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, 2*TWpx, 2*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft3.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, 3*TWpx, 2*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft4.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, 0, 3*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft5.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, TWpx, 3*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft6.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, 2*TWpx, 3*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    e = msk.readTGA("_model\\soft7.tga");
    if(e)  return e;
    msk.convolute(si);
    dst.draw( msk, 3*TWpx, 3*TWpx, TexImage::DRAWMODE_ALPHA_BLIT );

    _img_wall.create_from_template( dst, TARGET_BPP, false );
    _img_wall.draw(dst);
    _img_wall.writeRAW("data\\rwall.bwh");

    return 0;
}

////////////////////////////////////////////////////////////////////////////////
opcd C_World::load_anim()
{
    if( 0 != _aspr_explode.load("data\\anim\\explode\\1.csa", TARGET_BPP) )
    {
        _aspr_explode.compose( TARGET_BPP, "_model\\anim\\explode\\1\\manx", ".tga", 2, 1, 16 );
        _aspr_explode.save("data\\anim\\explode\\1.csa");
/*
        TexImage tmp;

        opcd e = tmp.compose( "_model\\anim\\explode\\1\\manx", ".tga", 2, 1, 4, 4 );
        if(e)  return e;

        _anim_explode.create_from_template(tmp,TARGET_BPP,false);
        _anim_explode.draw(tmp);
        _anim_explode.writeRAW("data\\anim\\explode\\1.bwh");
*/
    }

    return 0;
}

